package com.cr.crandroid.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cr.crandroid.R;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.utils.DateUtils;
import com.cr.crandroid.vo.CarVo;
import com.cr.crandroid.vo.RentVo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HomeAdapter extends BaseQuickAdapter<CarVo, BaseViewHolder> {

    private RentVo nowRent = new RentVo();
    private boolean isShow = false;

    public HomeAdapter(@Nullable List<CarVo> data) {
        super(R.layout.item_home, data);
    }


    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, CarVo carVo) {
        baseViewHolder.setText(R.id.textView, carVo.getBrand());
        baseViewHolder.setText(R.id.textView2, carVo.getColor());
        baseViewHolder.setText(R.id.textView3, carVo.getSeatingCapacity());
        ImageView imageView = baseViewHolder.getView(R.id.imageView2);
        TextView textView = baseViewHolder.getView(R.id.textView6);
        switch (carVo.getIsRent()) {
            case CommonConstant.NOT_REND:
                textView.setVisibility(View.VISIBLE);
                imageView.setImageResource(R.drawable.reserve);
                break;
            case CommonConstant.REND:
                textView.setVisibility(View.GONE);
                imageView.setImageResource(R.drawable.home_rent);
                break;
        }
        Date startDate;
        Date endDate;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate = new Date();
            startDate = sdf.parse(carVo.getRentalStart());
            endDate = sdf.parse(carVo.getRentalEnd());
            if (nowDate.getTime() < endDate.getTime()) {
                baseViewHolder.setText(R.id.textView4, "还有" + DateUtils.dateDiff(endDate, nowDate) + "天可租");
            }
            if (startDate.getTime() > nowDate.getTime()) {
                textView.setVisibility(View.GONE);
                imageView.setImageResource(R.drawable.be_start);
                baseViewHolder.setText(R.id.textView4, "还有" + DateUtils.dateDiff(startDate, nowDate) + "天开始");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (carVo.getImage() != null)
            Glide.with(getContext()).load(carVo.getImage().split(",")[0])
                    .into((ImageView) baseViewHolder.getView(R.id.imageView));
    }

    public RentVo getNowRent() {
        return nowRent;
    }
}
